#include "stdafx.h"
#include "Image.h"

Image::Image():w(0),h(0){}

Image::Image(int w1, int h1, char *filename1)
{
	filename = filename1;
	h = h1;
	w = w1;
	pic = new unsigned char[w*h*3];
}

Image::Image(unsigned char *pic1, int w1, int h1, char *filename1)
{
	pic = pic1;
	filename = filename1;
	h = h1;
	w = w1;
	pic = new unsigned char[w*h*3];
}

Image::~Image()
{
	delete[] pic;
}

unsigned char* Image::getPic()
{
	return pic;
}

char* Image::getFileName()
{
	return filename;
}

int Image::getWidth()
{
	return w;
}

int Image::getHeight()
{
	return h;
}

void Image::setPixel(int x, int y, Couleur couleurFond)
{
	pic[x*3+y*w*3] = (unsigned char) ((int)(couleurFond.getRouge()*255));
	pic[x*3+y*w*3+1] = (unsigned char) ((int)(couleurFond.getVert()*255));
	pic[x*3+y*w*3+2] = (unsigned char) ((int)(couleurFond.getBleu()*255));  
}

//ecrit un fichier .ppm
void Image::sauvegarder()
{
	FILE * pFile;
	
	fopen_s(&pFile, filename,"w");
	fprintf (pFile, "P3\r%d %d\r255\r", w, h);
	
	for (int i = 0; i < w*h*3 ; i+=3)
	{
		fprintf (pFile, "%d %d %d ", pic[i], pic[i+1], pic[i+2]);
	}
	fclose(pFile);
}

